package Arkkomeet

import org.apache.jasper.compiler.Node.ParamsAction;
import org.apache.tools.zip.ZipFile.OffsetEntry;
import org.hibernate.criterion.Order;
import org.junit.Before;
import org.springframework.dao.DataIntegrityViolationException

import arkkomeet.TipoReunionService;

class TipoReunionController {

	def tipoReunionService
	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def index() {
		redirect(action: "list", params: params)
	}

	def list(Integer max) {
		String j=params.treunionNombreBuscar
		def tipoReunionInstance= new TipoReunion()
		if(params.treunionNombreBuscar){
			tipoReunionInstance.setTreunionNombre(params.treunionNombreBuscar)
		}
		def listaTipoReunion= tipoReunionService.busquedaFiltrada(params)
		if(listaTipoReunion.size==0){
			params.max = Math.min(max ?: 10, 100)
			render (view:"list", model:[tipoReunionInstance:tipoReunionInstance,tipoReunionInstanceList: TipoReunion.listOrderByTreunionNombre(params),tipoReunionInstanceTotal:TipoReunion.count()])
			//[tipoReunionInstance:params ,tipoReunionInstanceList: TipoReunion.listOrderByTreunionNombre(params), tipoReunionInstanceTotal: TipoReunion.count()]
		}
		else{
			// tipoReunionInstance = params
			params.max = Math.min(max ?: 10, 100)
			render (view:"list", model:[tipoReunionInstance:tipoReunionInstance,tipoReunionInstanceList:listaTipoReunion,tipoReunionInstanceTotal:listaTipoReunion.size()])
			return
		}
	}

	def create(Integer max) {
		flash.menValNombre=""
		flash.mensage=""
		[tipoReunionInstance: new TipoReunion(params)]
		params.max=Math.min(max?:10,100)
		render (view:"create",model:[tipoReunionInstanceList: TipoReunion.listOrderByTreunionNombre(params), tipoReunionInstanceTotal:TipoReunion.count()])
	}

	def save(Integer max) {
		flash.menValNombre=""
		flash.mensage=""
		def tipoReunionInstance = new TipoReunion(params)
		params.max=Math.min(max?:10,100)
		if (!tipoReunionInstance.save(flush: true)) {
			if(params.treunionNombre.equals("")){
				flash.mensage= message(code:'TipoReunion.treunionNombre.blank')
			}
			else{
				if(message(code:'TipoReunion.treunionNombre.unique')){
					flash.menValNombre= message(code:'TipoReunion.treunionNombre.unique')
				}
			}
			render (view:"create", model:[tipoReunionInstance:tipoReunionInstance,tipoReunionInstanceList: TipoReunion.listOrderByTreunionNombre(params), tipoReunionInstanceTotal:TipoReunion.count()])
			return
		}
		flash.message = message(code: 'default.created.message', args: [message(code: 'tipoReunion.label', default: 'TipoReunion'), tipoReunionInstance.id])
		redirect(action: "show", id: tipoReunionInstance.id)
	}

	
	def show(Long id) {
		def tipoReunionInstance = TipoReunion.get(id)
		if (!tipoReunionInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'tipoReunion.label', default: 'TipoReunion'), id])
			redirect(action: "list")
			return
		}

		[tipoReunionInstance: tipoReunionInstance]
	}

	def edit(Long id) {
		def tipoReunionInstance = TipoReunion.get(id)
		if (!tipoReunionInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'tipoReunion.label', default: 'TipoReunion'), id])
			redirect(action: "list")
			return
		}

		[tipoReunionInstance: tipoReunionInstance]
	}

	def update(Long id, Long version) {
		flash.menValNombre=""
		flash.mensage=""
		def tipoReunionInstance = TipoReunion.get(id)
		if (!tipoReunionInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'tipoReunion.label', default: 'TipoReunion'), id])
			redirect(action: "list")
			return
		}

		if (version != null) {
			if (tipoReunionInstance.version > version) {
				tipoReunionInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
						[message(code: 'tipoReunion.label', default: 'TipoReunion')] as Object[],
						"Another user has updated this TipoReunion while you were editing")
				render(view: "edit", model: [tipoReunionInstance: tipoReunionInstance])
				return
			}
		}

		tipoReunionInstance.properties = params

		if (!tipoReunionInstance.save(flush: true)) {
			if(params.treunionNombre.equals("")){
				flash.mensage= message(code:'TipoReunion.treunionNombre.blank')
			}
			else{
				if(message(code:'TipoReunion.treunionNombre.unique')){
					flash.menValNombre= message(code:'TipoReunion.treunionNombre.unique')
				}
			}
			render(view: "edit", model: [tipoReunionInstance: tipoReunionInstance])
			return
		}

		flash.message = message(code: 'default.updated.message', args: [message(code: 'tipoReunion.label', default: 'TipoReunion'), tipoReunionInstance.id])
		redirect(action: "list", id: tipoReunionInstance.id)
	}

	def delete(Long id) {
		def tipoReunionInstance = TipoReunion.get(id)
		if (!tipoReunionInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'tipoReunion.label', default: 'TipoReunion'), id])
			redirect(action: "list")
			return
		}

		try {
			tipoReunionInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'tipoReunion.label', default: 'TipoReunion'), id])
			redirect(action: "list")
		}
		catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'tipoReunion.label', default: 'TipoReunion'), id])
			redirect(action: "show", id: id)
		}
	}
}
